package com.pomelo.kill.service.impl;

import com.pomelo.kill.common.Constants;
import com.pomelo.kill.service.GoodsService;
import com.pomelo.kill.service.RedisService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.pomelo.kill.model.Goods;
import com.pomelo.kill.mapper.GoodsMapper;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private RedisTemplate<String, List<Goods>> redisTemplate;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return goodsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Goods record) {
        return goodsMapper.insert(record);
    }

    @Override
    public int insertSelective(Goods record) {
        return goodsMapper.insertSelective(record);
    }

    @Override
    public Goods selectByPrimaryKey(Integer id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Goods record) {
        return goodsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Goods record) {
        return goodsMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Goods> selectAllKill(String date) {
        return goodsMapper.selectAllKill(date);
    }

    @Override
    public List<Goods> selectAll() {
        List<Goods> list = redisTemplate.opsForValue().get(Constants.REDIS_GOODS);
        if (list == null) {
            list = goodsMapper.selectAll();
            redisTemplate.opsForValue().set(Constants.REDIS_GOODS, list, 60, TimeUnit.MINUTES);
        }
        return list;
    }

}
